package com.project.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.github.pagehelper.PageInfo;
import com.project.bean.SaleClientBean;
import com.project.service.ISaleClientService;
import com.project.util.ExcelUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

/**
 * @author lyl
 * description:
 */
@RestController
@RequestMapping("saleClient")
@Api(tags = "客户销售统计")
public class SaleClientController {
    @Autowired
    private ISaleClientService iSaleClientService;

    @GetMapping("showByPage/{pageNum}/{clientName}/{startDate}/{endDate}")
    private PageInfo<SaleClientBean> showByPage(@PathVariable int pageNum,
                                                @PathVariable String clientName,
                                                @PathVariable String startDate,
                                                @PathVariable String endDate) {
        if ("1".equals(clientName)) {
            clientName = null;
        }
        return iSaleClientService.findByPage(pageNum, clientName, LocalDate.parse(startDate), LocalDate.parse(endDate));
    }

    @GetMapping("showAll")
    private List<SaleClientBean> showAll() {
        return iSaleClientService.showAll();
    }

    //导出接口
    @GetMapping("exportExcel")
    public void export(HttpServletResponse response) {
        System.out.println("开始导出");
        // 模拟从数据库获取需要导出的数据
        List<SaleClientBean> personList = iSaleClientService.showAll();
//        //设置序号（将id字段作为序号，导出后实现序号递增）
//        Integer i = 1;
//        for (SaleClientBean users : personList) {
//            users.setId(i++);
//        }
        // 导出操作
        ExcelUtil.exportExcel(personList, "sheet导出", "mySheet", SaleClientBean.class, SaleClientBean.class.getName() + ".xls", response);
    }

    @RequestMapping("importExcel")
    public String importExcel2(@RequestParam("file") MultipartFile file) {
        ImportParams importParams = new ImportParams();
        //表格标题行数,默认0
        importParams.setHeadRows(1);
        //表头行数,默认1
        importParams.setTitleRows(1);
        //是否需要校验上传的Excel,默认false
        importParams.setNeedVerify(false);
        try {
            ExcelImportResult<SaleClientBean> result = ExcelImportUtil.importExcelMore(file.getInputStream(), SaleClientBean.class, importParams);
            List<SaleClientBean> list = result.getList();
            for (SaleClientBean l : list) {
                System.out.println(l);
            }
            System.out.println("从Excel导入数据一共 {} 行 " + list.size());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } catch (Exception e1) {
            System.out.println("导入失败：{}" + e1.getMessage());
        }
        return "导入成功";
    }
}
